﻿package  {
	import flash.net.URLRequest;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	
	public class Preloader extends MovieClip{
		private var fileURL:String;
		private var contents:MovieClip;
		private var attachments:Array;
		
		private var issetProgressBar:Boolean = false;
		private var bar:MovieClip;
		
		private var issetPercentageShower:Boolean = false;
		private var percTxtField:TextField;

		public function Preloader(_fileURL:String) {
			fileURL = _fileURL;
			contents = new MovieClip();
			this.addChild(contents);
			attachments = new Array();
		}
		
		public function setLoadMovie(movie:MovieClip):void {
			contents.addChildAt(movie, 0);
			attachments.push(movie);
		}
		
		public function setProgressBar(bar_x:Number, bar_y:Number, BGcolor:Number, barColor:Number, barWidth:Number, barHeight:Number):void {
			issetProgressBar = true;
			var barContainer:MovieClip = new MovieClip();
			
			var barBG:MovieClip = new MovieClip();
			barBG.graphics.beginFill(BGcolor);
			barBG.graphics.drawRect(0, 0, barWidth, barHeight);
			barBG.graphics.endFill();
			
			bar = new MovieClip();
			bar.graphics.beginFill(barColor);
			bar.graphics.drawRect(0, 0, barWidth, barHeight);
			bar.graphics.endFill();
			bar.scaleX = 0.0;
			
			barContainer.addChild(barBG);
			barContainer.addChild(bar);
			
			attachments.push(barContainer);
			barContainer.x = bar_x;
			barContainer.y = bar_y;
			contents.addChild(barContainer);
		}
		
		public function setPercentageShower(perc_x:Number, perc_y:Number, color:Number, size:Number, fontFace:String, fontStyle:String):void {
			issetPercentageShower = true;
			var showerContainer:MovieClip = new MovieClip();
			
			var percTxtFormat:TextFormat = new TextFormat();
			percTxtFormat.align = 'center';
			percTxtFormat.color = color;
			percTxtFormat.size = size;
			percTxtFormat.font = fontFace;
			switch(fontStyle) {
				case 'italic':
					percTxtFormat.italic = true;
					break;
				case 'bold':
					percTxtFormat.bold = true;
					break;
				default:
					percTxtFormat.italic = false;
					percTxtFormat.bold = false;
					break;
			}
			
			percTxtField = new TextField();
			percTxtField.text = "0%";
			percTxtField.defaultTextFormat = percTxtFormat;
			
			showerContainer.addChild(percTxtField);
			
			attachments.push(showerContainer);
			showerContainer.x = perc_x;
			showerContainer.y = perc_y;
			contents.addChild(showerContainer);
		}
		
		public function startLoad():void {
			var genericLoader:Loader = new Loader();
			genericLoader.load(new URLRequest(fileURL));
			genericLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loading);
			genericLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, showContent);

			function loading(e:ProgressEvent):void {
				var perc:int = (e.bytesLoaded / e.bytesTotal) * 100;
				if(issetProgressBar) {
					bar.scaleX = e.bytesLoaded / e.bytesTotal;
				}
				if(issetPercentageShower) {
					percTxtField.text = String(perc) + "%";
				}
			}
			
			function showContent(e:Event):void {
				genericLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loading);
				genericLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, showContent);
				
				for(var j:int = 0; j < attachments.length; j++) {
					contents.removeChild(attachments[j]);
				}
				contents.addChild(genericLoader);
			}
		}
	}
}